﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// FoodsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FoodsWindow : Window
    {
        public FoodsWindow()
        {
            InitializeComponent();
           
        }
        public string lang { get; set; }
        SQLiteHelper sq = new SQLiteHelper();

        bool flag = true;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string nm=btn.Name;
            switch (nm)
            {
                case "btn_choose":

                    data = new DataTable();
                    data.Columns.Add("id");
                    data.Columns.Add("name");

                    for (int i = 0; i < dgv1.Items.Count; i++)
                    {

                        
                        DataRowView row = (DataRowView)dgv1.Items[i];

                        string id = row.Row.ItemArray[0].ToString();
                        string name = row.Row.ItemArray[1].ToString();
                        //获取该行的某列
                        CheckBox cb = (CheckBox)dgv1.Columns[0].GetCellContent(row);

                        bool? b=cb.IsChecked;
                        bool c = false;
                        bool bb = Boolean.TryParse(b.ToString(), out c);

                     //   Console.WriteLine("bool:"+b+" id:"+id+" name:"+name);

                        if (c)
                        {
                            DataRow r = data.NewRow();
                            r["id"] = id;
                            r["name"] = name;
                            data.Rows.Add(r);
                        }

                       

                    }

                    this.Close();

                    break;

                case "btn_choose_all":

                   

                    for (int i = 0; i < dgv1.Items.Count; i++)
                    {


                        DataRowView row = (DataRowView)dgv1.Items[i];

                        string id = row.Row.ItemArray[0].ToString();
                        string name = row.Row.ItemArray[1].ToString();
                        //获取该行的某列
                        CheckBox cb = (CheckBox)dgv1.Columns[0].GetCellContent(row);

                         
                        if (flag)
                        {
                            cb.IsChecked = true;
                        }
                        else
                        {
                            cb.IsChecked = false;
                        }

                        

                    }
                    flag = !flag;


                    break;
                case "btn_close":
                    this.Close();
                    break;
            }

        }

        public DataTable data = new DataTable();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            setlang();
            loadGrid();
        }

        private void loadGrid()
        {
             
            DataTable data = sq.getData("select  * from food where lang='" + lang + "' ");
            dgv1.ItemsSource = data.DefaultView;
        }


        private void setlang()
        {
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                col_check.Header = "تاللاش";
                col_food_name.Header = "ئىسمى";
                
                btn_choose.Content = "تامام";
                btn_choose_all.Content = "ھەممىنى تاللاش";
                btn_close.Content = "چېكىنىش";
            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                col_check.Header = "选择";
                col_food_name.Header = "名称";

                btn_choose.Content = "完成";
                btn_choose_all.Content = "选择全部";
                btn_close.Content = "退出";

            }
        }

       
    }
}
